package com.addplus.townmall.server.queue.serviceimpl;

import com.addplus.townmall.server.api.mapper.town.*;
import com.addplus.townmall.server.api.model.town.*;
import com.addplus.townmall.server.api.modelenum.PlatformEnum;
import com.addplus.townmall.server.api.service.queue.VistitRecordService;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 类名: VistitRecordService
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/10/19 5:57 PM
 * @description 类描述: 浏览记录异步实现类
 */
@Service(interfaceClass = VistitRecordService.class, async = true)
public class VistitRecordServiceImpl implements VistitRecordService {

    @Autowired
    private ShopProductMapper shopProductMapper;

    @Autowired
    private ShopVisitMapper shopVisitMapper;

    @Autowired
    private StoryArticleMapper storyArticleMapper;

    @Autowired
    private StoryReadRecoredMapper storyReadRecoredMapper;

    @Autowired
    private StoreDetailMapper storeDetailMapper;

    @Autowired
    private StoreVisitMapper storeVisitMapper;

    @Autowired
    private GiftCardMapper giftCardMapper;

    @Autowired
    private ShopProductFightMapper shopProductFightMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void visteRecordAdd(Integer memberId, Integer objId, Integer type, PlatformEnum platformEnum) {
        if (DataUtils.EmptyOrNegative(objId, type)) {
            return;
        }
        Date date = new Date();
        if (type == 0) {
            //更新商品浏览记录
            UpdateWrapper<ShopProduct> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("visit_count = visit_count+1");
            updateWrapper.eq("id", objId);
            updateWrapper.set("gmt_modified", date);
            shopProductMapper.update(new ShopProduct(), updateWrapper);
            if (!DataUtils.EmptyOrNegativeOrZero(memberId)) {
                shopVisitAadd(memberId, objId, platformEnum, date);
            }
        } else if (type == 1) {
            // 更新店铺记录
            UpdateWrapper<StoreDetail> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("visit_count = visit_count+1");
            updateWrapper.eq("id", objId);
            updateWrapper.set("gmt_modified", date);
            storeDetailMapper.update(new StoreDetail(), updateWrapper);
            if (!DataUtils.EmptyOrNegativeOrZero(memberId)) {
                //商品添加浏览
                StoreVisit storeVisit = new StoreVisit();
                storeVisit.setStoreId(objId);
                storeVisit.setMemberId(memberId);
                storeVisit.setPlatform(platformEnum.getType());
                storeVisit.setIsDeleted(0);
                storeVisit.setGmtCreate(date);
                storeVisit.setGmtModified(date);
                storeVisitMapper.insert(storeVisit);
            }
        } else if (type == 2) {
            //更新文章信息
            UpdateWrapper<StoryArticle> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("readcount_true = readcount_true+1");
            updateWrapper.eq("id", objId);
            updateWrapper.set("gmt_modified", date);
            storyArticleMapper.update(new StoryArticle(), updateWrapper);
            if (!DataUtils.EmptyOrNegativeOrZero(memberId)) {
                //商品添加浏览
                StoryReadRecored storyReadRecored = new StoryReadRecored();
                storyReadRecored.setStoryArticleId(objId);
                storyReadRecored.setMemberId(memberId);
                storyReadRecored.setIsDelete(0);
                storyReadRecored.setGmtCreate(date);
                storyReadRecored.setGmtModified(date);
                storyReadRecoredMapper.insert(storyReadRecored);
            }
        } else if (type == 3) {
            //更新礼品记录
            UpdateWrapper<GiftCard> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("visit_count = visit_count+1");
            updateWrapper.eq("id", objId);
            updateWrapper.set("gmt_modified", date);
            giftCardMapper.update(new GiftCard(), updateWrapper);
        } else if (type == 4) {
            //更新商品浏览记录
            UpdateWrapper<ShopProductFight> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("visit_count = visit_count+1");
            updateWrapper.eq("id", objId);
            updateWrapper.set("gmt_modified", date);
            shopProductFightMapper.update(new ShopProductFight(), updateWrapper);
            if (!DataUtils.EmptyOrNegativeOrZero(memberId)) {
                shopVisitAadd(memberId, objId, platformEnum, date);
            }
        }
    }


    private void shopVisitAadd(Integer memberId, Integer objId, PlatformEnum platformEnum, Date date) {
        //商品添加浏览
        ShopVisit shopVisit = new ShopVisit();
        shopVisit.setShopId(objId);
        shopVisit.setMemberId(memberId);
        shopVisit.setPlatform(platformEnum.getType());
        shopVisit.setIsDeleted(0);
        shopVisit.setGmtCreate(date);
        shopVisit.setGmtModified(date);
        shopVisitMapper.insert(shopVisit);
    }
}
